package com.app.library.utils.glide

import android.content.Context
import com.blankj.utilcode.util.SDCardUtils
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.engine.cache.ExternalPreferredCacheDiskCacheFactory
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory
import com.bumptech.glide.load.engine.cache.LruResourceCache
import com.bumptech.glide.module.AppGlideModule

@GlideModule
class CustomGlideModule : AppGlideModule() {

    override fun applyOptions(context: Context, builder: GlideBuilder) {
        //设置缓存大小为100mb
        val memoryCacheSizeBytes = 1024 * 1024 * 100
        //设置内存缓存大小
        builder.setMemoryCache(LruResourceCache(memoryCacheSizeBytes.toLong()))
        //根据SD卡是否可用选择是在内部缓存还是SD卡缓存
        if (SDCardUtils.isSDCardEnableByEnvironment()) {
            builder.setDiskCache(
                ExternalPreferredCacheDiskCacheFactory(
                    context,
                    "Maxity",
                    memoryCacheSizeBytes.toLong()
                )
            )
        } else {
            builder.setDiskCache(
                InternalCacheDiskCacheFactory(
                    context,
                    "Maxity",
                    memoryCacheSizeBytes.toLong()
                )
            )
        }
    }

    //    针对V4用户可以提升速度
    override fun isManifestParsingEnabled(): Boolean {
        return false
    }
}